package com.algorithm.ch1.yjx.array;

/**
 * @author yuanjianxin
 * @date 2018/8/15 11:16
 * 太难了，爸爸解不出来啊
 */
public class Rotate90 {

    public static void main(String[] args) {
        int[][] arr = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
        int[][] arr1 = {
                {5, 1, 9, 11},
                {2, 4, 8, 10},
                {13, 3, 6, 7},
                {15, 14, 12, 16}
        };

        rotate(arr);
    }

    public static void rotate(int[][] matrix) {
        System.out.println("调换前==");
        print(matrix);

        int length = matrix.length;
        // 调换对角元素。循环上部分倒三角形
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - i; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[length - j - 1][length - i - 1];
                matrix[length - j - 1][length - i - 1] = tmp;
            }
        }

        System.out.println("对角元素调换后==");
        print(matrix);

        // 调换列元素
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length / 2; j++) {
                int tmp = matrix[j][i];
                matrix[j][i] = matrix[length - j - 1][i];
                matrix[length - j - 1][i] = tmp;
            }
        }

        System.out.println("列元素调换后==");
        print(matrix);
    }

    public static void print(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print("[");
            for (int j = 0; j < arr.length; j++) {
                if (j == arr.length - 1) {
                    System.out.print(arr[i][j]);
                } else {
                    System.out.print(arr[i][j] + ",");
                }
            }
            System.out.println("]");
        }
    }
}
